package com.oa.role.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oa.organization.dao.OrganizationDao;
import com.oa.organization.entity.Organization;
import com.oa.role.dto.RoleDto;
import com.oa.system.dao.GroupDao;
import com.oa.system.dao.RoleDao;
import com.oa.system.dao.UserInfoDao;
import com.oa.system.entity.Group;
import com.oa.system.entity.Role;
import com.oa.system.entity.UserInfo;
import com.oa.user.dto.UserInfoDto;

@Service
@Transactional
public class RoleManagerServiceImpl implements RoleManagerService {
	@Resource
	private UserInfoDao userInfoDao;
	@Resource
	private RoleDao roleDao;
	@Resource
	private GroupDao groupDao;
	@Resource
	private OrganizationDao organizationDao;

	@Override
	@Transactional(readOnly = true)
	public Iterable<Group> findAllGroup() {
		return groupDao.findAll();
	}

	@Override
	@Transactional(readOnly = true)
	public Iterable<Role> findAllRole() {
		return roleDao.findAll();
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Role> findAllRole(Pageable pageable) {
		return roleDao.findAll(pageable);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<RoleDto> findAllUser(Pageable pageable) {
		Page<UserInfo> userinfoPage = userInfoDao.findAll(pageable);
		Page<RoleDto> roleDtoPage = userinfoPage.map(new Converter<UserInfo, RoleDto>() {
			@Override
			public RoleDto convert(UserInfo source) {
				return RoleDto.toRoleDto(source);
			}
		});

		return roleDtoPage;
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Role> searchAllRole(Specification<Role> spec, Pageable pageable) {
		return roleDao.findAll(spec, pageable);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<RoleDto> searchAllUser(UserInfoDto userInfoDto, Pageable pageable) {
		if (userInfoDto.getDept() != null && userInfoDto.getDept().trim().length() > 0) {
			Organization organization = organizationDao.findByName(userInfoDto.getDept());
			userInfoDto.setOrganization(organization);
		}

		if (userInfoDto.getRole() != null && userInfoDto.getRole().trim().length() > 0) {
			Role role = roleDao.findByRole(userInfoDto.getRole());
			userInfoDto.setRoleOwn(role);
		}

		Page<UserInfo> userinfoPage = userInfoDao.findAll(UserInfoDto.getUserManagerQuery(userInfoDto), pageable);
		Page<RoleDto> roleDtoPage = userinfoPage.map(new Converter<UserInfo, RoleDto>() {
			@Override
			public RoleDto convert(UserInfo source) {
				return RoleDto.toRoleDto(source);
			}
		});

		return roleDtoPage;
	}

	@Override
	public Role saveOrUpdateRole(Role role, List<Long> groups) {
		if (groups != null) {
			role.getGroup().clear();
			roleDao.save(role);

			Group groupEntity;
			for (Long group : groups) {
				groupEntity = groupDao.findOne(group);
				role.getGroup().add(groupEntity);
			}
		}

		return roleDao.save(role);
	}

	@Override
	public void updateUserRole(Long id, Long roleId) {
		UserInfo userInfo = userInfoDao.findOne(id);
		Role role = roleDao.findOne(roleId);
		userInfo.setRole(role);
		userInfoDao.save(userInfo);
	}

	@Override
	public void deleteRole(Long id) {
		Role role = roleDao.findOne(id);
		if (role != null) {
			role.setGroup(null);
			List<UserInfo> userInfos = userInfoDao.findByRoleId(id);
			for (UserInfo userInfo : userInfos) {
				userInfo.setRole(null);
			}
			userInfoDao.save(userInfos);
			roleDao.delete(id);
		}
	}

	@Override
	public void deleteRole(List<Long> ids) {
		Role role;
		List<UserInfo> userInfos = new ArrayList<>();
		for (Long id : ids) {
			role = roleDao.findOne(id);
			if (role != null) {
				role.setGroup(null);
				userInfos = userInfoDao.findByRoleId(id);
				for (UserInfo userInfo : userInfos) {
					userInfo.setRole(null);
				}
				userInfoDao.save(userInfos);
				roleDao.delete(id);
			}
		}
	}

}
